#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 1e6 + 10 , M = 5*N;
int h[N] , w[M] , e[N] , dis[N] , ne[M] , idx;
bool st[N];
int n , m , s;
priority_queue<PII,vector<PII>,greater<PII>> pq;

void add(int a , int b , int c)
{
    e[idx] = b ,w[idx] = c, ne[idx] = h[a] , h[a] = idx++;
}
void dijkstra()
{
    memset(dis , 0x3f , sizeof dis);
    dis[s] = 0;
    pq.push({0,s});
    while(!pq.empty())
    {
        auto t = pq.top();
        pq.pop();
        auto ver = t.second , distance = t.first;
        if(st[ver]) continue;
        st[ver] = true;
        for(int i = h[ver] ; i != -1 ; i = ne[i])
        {
            int j = e[i];
            if(!st[j])
            {
                dis[j] = min(dis[j] , distance + w[i]);
                pq.push({dis[j],j});
            }
        }
    }
}
int main()
{
    cin >> n >> m >> s;
    memset(h,-1,sizeof h);
    memset(w,0x3f,sizeof w);
    for(int i = 0 ; i < m ; ++i)
    {
        int a , b , c;
        cin >> a >> b >> c;
        add(a,b,c);
    }
    dijkstra();
    for(int i = 1 ; i <= n ; ++i)printf("%d ",dis[i]);
    printf("\n");
    return 0;
}